// nodejs 中的path模块
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin')

// 静态文件页面
const files = fs.readdirSync(path.resolve(__dirname, '../static/src/pages'));

let config = {
    // 入口文件，path.resolve()方法，可以结合我们给定的两个参数最后生成绝对路径，最终指向的就是我们的index.js文件
    // entry: path.resolve(__dirname, '../static/src/main.js'),
    entry: (function(){
        let entry_obj = {};
        files.forEach(pg=>{
            entry_obj[pg] = path.resolve(__dirname, `../static/src/pages/${pg}/${pg}.js`)
        })
        // vendor: ['vue'],
        return entry_obj
    })(),
    // 输出配置
    output: {
        path: path.resolve(__dirname, '../static/dist'),
        publicPath: '/dist/',
        filename: '[name].build.js'
    },
    resolve: {
        extensions: ['.js', '.vue'],
    //     alias: {
    //         'Vue': 'vue/dist/vue.js'
    //     }
    },
    // devServer: {
    //     historyApiFallback: true,
    //     noInfo: true
    // },
    devtool: '#eval-source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use:{
                    loader:'babel-loader',
                    options:{
                        presets:['env'],
                        plugins:['transform-runtime']
                    }
                }
            },
            {test:/\.vue$/,use:'vue-loader'},
            {test:/\.css$/,use:['style-loader','css-loader']},
            {test:/\.scss$/,use:['style-loader','css-loader','sass-loader']},
            // {
            //     test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            //     loader: 'url-loader',
            //     options: {
            //         limit: 10000,
            //         name:'assets/images/[name].[hash:7].[ext]'
            //     }
            // },
        ]
    },
    plugins: [
        // new HtmlWebpackPlugin({
        //     filename: '../../app/pages/index.html',
        //     chunks: ['index'],
        //     // template: path.resolve(__dirname, '../static/src/index.html'),
        //     inject: true
        // }),
        // new HtmlWebpackPlugin({
        //     filename: '../../app/pages/about.html',
        //     chunks: ['about'],
        //     // template: path.resolve(__dirname, '../static/src/about.html'),
        //     inject: true
        // }),
        //热替换
        // new webpack.HotModuleReplacementPlugin(),
    ]
}
// 生成输出文件
files.forEach(file=>{
    // let name = file.match(/^\w+/g)
    if(file){
        config.plugins.push(
            new HtmlWebpackPlugin({
                filename: `../../app/pages/${ file }.ejs`,
                chunks: [`${ file }`],
                template: path.resolve(__dirname, '../static/src/base.html'),
                inject: true
            }),
        )
    }
})

//热替换
config.plugins.push(new webpack.HotModuleReplacementPlugin())

module.exports = config;